package com.example.pictureview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.example.pictureview.widget.ImageLoader;
import com.example.pictureview.widget.NineGridLayout;
import com.example.pictureview.widget.PictureDialog;

import java.util.List;

/**
 * Created by Administrator on 2017/9/20 0020.
 */

public class Image9Adapter extends BaseAdapter implements NineGridLayout.OnItemClickListener {
    private final List<List<String>> mPaths;

    private final Context mContext;
    private final ImageLoader mImageLoader;
    private PictureDialog mDialog;

    public Image9Adapter(Context context, ImageLoader imageLoader, List<List<String>> path) {
        mContext = context;
        mImageLoader = imageLoader;
        mPaths = path;
        mDialog = new PictureDialog(context, mImageLoader);
    }

    @Override
    public int getCount() {
        return mPaths.size();
    }

    @Override
    public List<String> getItem(int position) {
        return mPaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        View view = null;
//        if ((view = convertView) == null) {
//            view = View.inflate(mContext, R.layout.item_9_image, null);
//        }
//        ImageView imageView = (ImageView) view.findViewById(R.id.item_imageview);

        NineGridLayout nineGridLayout = null;
        if ((nineGridLayout = (NineGridLayout) convertView) == null) {
            nineGridLayout = new NineGridLayout(mContext);
            nineGridLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            nineGridLayout.setOnItemClickListener(this);
        }
        nineGridLayout.setUrlList(getItem(position), mImageLoader);
        return nineGridLayout;
    }


    @Override
    public void onItemClick(ViewGroup group, View view, int position, List<String> urls) {
        if (!mDialog.isShowing()) {
            mDialog.setImageUrls(position, urls);
            mDialog.show();
        }
    }
}
